#!/bin/sh
################################################################################
# dhcp
################################################################################
. /etc/rc.d/functions

start() {

    local DHCLIENT_CONF
    local INTERFACE
    local INTERFACE_ITEM
    local INTERFACE_LIST
    local IP_ADDRESS

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/dhcp] Starting..."

    UDHCPC_CONF='/etc/udhcpc.conf'

    # Create a 'udhcpc.conf' file.
    /bin/rm -f "${UDHCPC_CONF}~"
    /bin/mkdir -p "`/usr/bin/dirname ${UDHCPC_CONF}~`"
    /bin/touch "${UDHCPC_CONF}~"
    /bin/chmod 644 "${UDHCPC_CONF}~"
    set | /bin/grep -e '^MM_DHCP_' > "${UDHCPC_CONF}~"
    /bin/mv -f "${UDHCPC_CONF}~" "${UDHCPC_CONF}"

    # If 'udhcpc' is running, then stop it.
    if [ -n "`/bin/pidof udhcpc`" ] ; then
        mm_message_output info "stopping DHCP client ..."
        while [ -n "`/bin/pidof udhcpc`" 2> /dev/null ] ; do
            /usr/bin/killall udhcpc
        done
    fi

    # Start 'udhcpc'.
    if [ -z "`/bin/pidof udhcpc`" ] ; then
        mm_message_output info "starting DHCP client ..."
        INTERFACE=${MM_NETWORK_INTERFACE}
        if [ -z "${INTERFACE}" ] ; then
            # Locate a connected network interface.
            # We use the first connected network interface found.
            INTERFACE_LIST=
            if [ -e /sys/class/net ] ; then
                INTERFACE_LIST=`/bin/ls -1 /sys/class/net | /bin/grep -v '^lo$'`
            fi
            INTERFACE=
            for INTERFACE_ITEM in ${INTERFACE_LIST} ; do
                /usr/sbin/ifplugstatus -q ${INTERFACE_ITEM}
                if [ $? -eq 2 ] ; then
                    INTERFACE=${INTERFACE_ITEM}
                    break
                fi
            done
        fi
        if [ -z "${INTERFACE}" ] ; then
            INTERFACE='eth0'
        fi

        # Waiting 15sec for Eth interface. On some platforms apperaing Eth needs some time...
        i=0
        while [ $i -lt 15 ] ; do
            no_iface=`/sbin/ifconfig ${INTERFACE} | /bin/grep "${INTERFACE}"`
            i=$((${i} + 1))
            if [ -z ${no_iface} ] ; then
                if [ $i -eq 15 ] ; then
                    mm_message_output err "error: After 15sec still no '${INTERFACE}' interface in system..."
                    /usr/bin/logger -t minimyth -p "local0.err" "[init.d/dhcp] Timeout waiting for ${INTERFACE} interface..."
                    /bin/false
                    exit 1
                else
                    /sbin/ifconfig ${INTERFACE} > /var/log/eth-interface
                    /usr/bin/logger -t minimyth -p "local0.info" "[init.d/dhcp] Waiting for ${INTERFACE} interface..."
                    /bin/sleep 1
                fi
            else
                /usr/bin/logger -t minimyth -p "local0.info" "[init.d/dhcp] ${INTERFACE} present. Starting DHCP client..."
                i=15
            fi
        done

        # Start DHCP on the interface.
        IP_ADDRESS=`/sbin/ifconfig ${INTERFACE} | /bin/grep '^ *inet addr:' | /bin/sed 's%^ *inet addr:\([^ ]*\) .*%\1%'`
        if [ -z "${IP_ADDRESS}" ] ; then
            /sbin/udhcpc -S -p /var/run/udhcpc.pid -s /etc/udhcpc.script -i ${INTERFACE} -O tzdbstr -O logsrv  > /var/log/udhcpc 2>&1
        else
            /sbin/udhcpc -S -p /var/run/udhcpc.pid -s /etc/udhcpc.script -i ${INTERFACE} -r ${IP_ADDRESS}  -O tzdbstr -O logsrv > /var/log/udhcpc 2>&1
        fi
        if [ $? -ne 0 ] ; then
            mm_message_output err "error: DHCP on interface '${INTERFACE}' failed."
            exit 1
        fi

        # Make sure we got an IP address.
        IP_ADDRESS=`/sbin/ifconfig ${INTERFACE} | /bin/grep '^ *inet addr:' | /bin/sed 's%^ *inet addr:\([^ ]*\) .*%\1%'`
        if [ -z "${IP_ADDRESS}" ] ; then
            mm_message_output err "error: Can't get IP from DHCP on interface '${INTERFACE}' ..."
            exit 1
        fi

    fi

    HOST_NAME=`/bin/hostname`
    if [ "x${HOST_NAME}" = "x?" ] ; then

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/dhcp] NOTICE:    !!! No hostname assigned by DHCP server !!!"

        # Add last 2 bytes from MAC as suffix
        # SUFFIX=`cat /sys/class/net/${INTERFACE}/address | sed -e "s/\(.*\):\(.*\):\(.*\):\(.*\):\(.*\):\(.*\)/\6/"`
        # Add full MAC as suffix
        SUFFIX=`cat /sys/class/net/${INTERFACE}/address | sed -e "s/://g"`

        if [ -z "${SUFFIX}" ] ; then
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/dhcp] NOTICE:    Can't determine MAC address on ${INTERFACE}. Will use \"Default\" suffix..."
            HOST_NAME="Myth-Frontend-Default"
        else
            HOST_NAME="Myth-Frontend-${SUFFIX}"
            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/dhcp] NOTICE:    !!! Hostname will be auto-set to \"${HOST_NAME}\" !!!"
        fi

        /bin/hostname ${HOST_NAME}

    fi

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/dhcp] Started with return code=0..."

    return 0
}

stop() {

    mm_message_output info "stopping DHCP client ..."
    if [ -n "`/bin/pidof udhcpc`" ] ; then
        /usr/bin/killall udhcpc
    fi

    return 0
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
